package com.yicun.road.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yicun.road.service.mapper.TDefectsPitsMapper;
import com.yicun.road.service.mapper.TShapesMapper;
import com.yicun.road.service.pojo.disease.TDefectsPits;
import com.yicun.road.service.pojo.disease.TShapes;
import com.yicun.road.service.service.DiseaseService;
import com.yicun.road.service.util.CheckOutException;
import com.yicun.road.service.util.CommonUtil;
import com.yicun.road.service.util.PageUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class DefectsPitsServiceImpl extends ServiceImpl<TDefectsPitsMapper, TDefectsPits> implements DiseaseService {
    @Resource
    private TDefectsPitsMapper tDefectsPitsMapper;
    @Resource
    private TShapesMapper tShapesMapper;
    @Resource
    private PageUtil pageUtil;

    @Override
    public Page<TDefectsPits> queryTDefectsPitsList(TDefectsPits tDefectsPits) {
        return tDefectsPitsMapper.queryTDefectsPitsList(pageUtil.defaultPage(tDefectsPits.getCurrPage(), tDefectsPits.getPageSize()), tDefectsPits);
    }

    @Override
    public void insertTDefectsPits(TDefectsPits tDefectsPits) {
        tDefectsPits.setCreateTime(new Date());
        tDefectsPits.setDeleted(0);
        tDefectsPits.setPitsCode(CommonUtil.genId());
        tDefectsPitsMapper.insert(tDefectsPits);
        //插入目标信息数据
        insertTShapes(tDefectsPits);
    }

    @Override
    public void updateTDiseasePits(TDefectsPits tDefectsPits) {
        tDefectsPits.setUpdateTime(new Date());
        tDefectsPits.setDeleted(0);
        tDefectsPitsMapper.updateById(tDefectsPits);
        //修改目标信息数据
        tShapesMapper.deleteByPrimaryKey(tDefectsPits.getId());
        insertTShapes(tDefectsPits);
    }

    private void insertTShapes(TDefectsPits tDefectsPits) {
        TShapes tShapes = new TShapes();
        if (!ObjectUtils.isEmpty(tDefectsPits)) {
            tShapes.setDeleted(0);
            tShapes.setCreateTime(new Date());
            tShapes.setLabel(tDefectsPits.getLabel());
            tShapes.setShapeType(tDefectsPits.getShapeType());
            tShapes.setDefectsId(tDefectsPits.getId());
            tShapesMapper.insert(tShapes);
        }
    }

    @Override
    public void deleteTDiseasePits(Integer id) {
        //查询数据是否存在
        TDefectsPits selectById = tDefectsPitsMapper.selectById(id);
        if (ObjectUtils.isEmpty(selectById)) throw new CheckOutException("数据不存在，请重试！");
        tDefectsPitsMapper.deleteById(id);
        //删除目标信息表中相关联的数据(t_shapes)
        tShapesMapper.deleteByPrimaryKey(id);
    }


}
